﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIS.ENT;
using SIS.BLL;


public partial class school_standard_level : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //LoadSchoolList();
            ddlSchoolList.DataBind();
            if (ddlSchoolList.Items.Count == 0)
            {
                lblMessage.Text = "Please set up a school profile firstly!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }

            if (ddlSchoolList.Items.Count > 0)
            {
                ddlSchoolList.Text = System.Configuration.ConfigurationManager.AppSettings["SchoolKeyDefault"].ToString();

                //ddlSchoolList.SelectedIndex = 0;
                //ddlSchoolList_SelectedIndexChanged(sender, e);
            }
        }
    }

    //private void LoadSchoolList()
    //{
    //    ddlSchoolList.Items.Clear();
    //    SchoolProfileTableList _profileList = SchoolProfileTableManager.GetList();
    //    if (_profileList != null)
    //    {
    //        foreach (SchoolProfileTable _profile in _profileList)
    //        {
    //            ddlSchoolList.Items.Add(new ListItem(_profile.SchoolName, _profile.SchoolKey.ToString()));
    //        }
    //    }
    //}

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            StandardLevelTable _standardLevel = new StandardLevelTable();
            _standardLevel.SchoolKey = Convert.ToInt32(lblSchoolKey.Text);
            _standardLevel.SchoolMajorKey = Convert.ToInt32(lblSchoolMajorKey.Text);
            _standardLevel.StandardLevelKey = Convert.ToInt32(lblStandardLevelKey.Text);
            _standardLevel.StandardLevelCode = txtStandardLevelCode.Text;
            _standardLevel.Description = txtDescription.Text;
            _standardLevel.Status = chkStatus.Checked;
            int _standardLevelKey = StandardLevelTableManager.Save(_standardLevel);
            if (_standardLevelKey > 0)
            {
                ClearFields();
                grvStandardLevel.DataBind();
                lblMessage.Text = "Saved sussccessfully!";
                lblMessage.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblMessage.Text = "Saving failed!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
    }

    protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlSchoolMajor.DataBind();
        grvStandardLevel.DataBind();
        ClearFields();
    }


    protected void btnShowStandardLevel_Click(object sender, EventArgs e)
    {

    }


    protected void grvStandardLevel_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
        if (grvStandardLevel.SelectedIndex >= 0)
        {
            DataKey _selectedDataKey = grvStandardLevel.SelectedDataKey;
            int _standardLevelKey = Convert.ToInt32(_selectedDataKey[0]);
            int _schoolKey = Convert.ToInt32(_selectedDataKey[1]);
            int _schoolMajorKey = Convert.ToInt32(_selectedDataKey[2]);
            if (_standardLevelKey > 0 && _schoolKey > 0)
            {
                lblSchoolKey.Text = _schoolKey.ToString();
                lblStandardLevelKey.Text = _standardLevelKey.ToString();
                lblSchoolMajorKey.Text = _schoolMajorKey.ToString();
                GridViewRow _selectedRow = grvStandardLevel.SelectedRow;
                txtStandardLevelCode.Text = _selectedRow.Cells[3].Text;
                txtDescription.Text = _selectedRow.Cells[4].Text;
                //lblMessage.Text = _selectedRow.Cells[4].Text;
                CheckBox _chkStatus = (CheckBox)(_selectedRow.Cells[5].Controls[0]);
                chkStatus.Checked = _chkStatus.Checked;
                //lblMessage.Text = _chkStatus.Checked.ToString();
            }
        }
    }

    private void ClearFields()
    {
        lblMessage.Text = "";

        lblStandardLevelKey.Text = "-1";
        lblSchoolMajorKey.Text = ddlSchoolMajor.Text;
        lblSchoolKey.Text = ddlSchoolList.Text;
        txtStandardLevelCode.Text = "";
        txtDescription.Text = "";
        chkStatus.Checked = false;
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        ClearFields();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (lblStandardLevelKey.Text.Length > 0)
        {
            if (StandardLevelTableManager.Delete(Convert.ToInt32(lblStandardLevelKey.Text)))
            {
                ClearFields();
                grvStandardLevel.DataBind();
                lblMessage.Text = "Item was deleted!";
                lblMessage.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblMessage.Text = "Deleting failed!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
        else
        {
            lblMessage.Text = "Please select an item to delete.";
            lblMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void ddlSchoolMajor_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
    }
}
